﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ledict.ConditionExtract.Controls
{
    public partial class colColumnFileter : Panel
    {
        public colColumnFileter(Font font, table tb)
        {
            InitializeComponent();
            CreateControls(font, tb);
        }

        public colColumnFileter(Font font, cause ca)
        {
            InitializeComponent();
            CreateControls(font,ca);
        }

        public colColumnFileter(Font font, element el)
        {
            InitializeComponent();
            CreateControls(font, el);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
        public delegate void DeleteButtonClick(object sender);

        public event DeleteButtonClick DeleteBtnClicked;

        public TextBox m_table = new TextBox();
        public TextBox m_field = new TextBox();
        public TextBox m_value = new TextBox();

        public TextBox m_causeId = new TextBox();

        public TextBox m_elementXpath = new TextBox();
        public TextBox m_elementValue = new TextBox();

        private void CreateControls(Font font, table tb)
        {
            Graphics graphics = CreateGraphics();
            Label lable1 = new Label();
            lable1.Left = this.Location.X;
            lable1.Text = "表";
            SizeF sizeF = graphics.MeasureString(lable1.Text, font);
            lable1.Width = (int)(Math.Ceiling(sizeF.Width));
            lable1.Height = (int)(Math.Ceiling(sizeF.Height));
            lable1.Top = 3;
            this.Controls.Add(lable1);
            
            m_table.Left = lable1.Right + 5;
            m_table.Top = this.Location.Y;
            m_table.Width = 140;
            m_table.Height = lable1.Height;
            this.Controls.Add(m_table);

            Label lable2 = new Label();
            lable2.Left = m_table.Right + 5;
            lable2.Text = "字段";
            sizeF = graphics.MeasureString(lable2.Text, font);
            lable2.Width = (int)(Math.Ceiling(sizeF.Width)) + 1;
            lable2.Height = (int)(Math.Ceiling(sizeF.Height));
            lable2.Top = 3;
            this.Controls.Add(lable2);
            
            m_field.Left = lable2.Right + 5;
            m_field.Top = this.Location.Y;
            m_field.Width = 140;
            m_field.Height = lable2.Height;
            this.Controls.Add(m_field);

            Label lable3 = new Label();
            lable3.Left = m_field.Right + 5;
            lable3.Text = "值";
            sizeF = graphics.MeasureString(lable3.Text, font);
            lable3.Width = (int)(Math.Ceiling(sizeF.Width)) + 1;
            lable3.Height = (int)(Math.Ceiling(sizeF.Height));
            lable3.Top = 3;
            this.Controls.Add(lable3);
            
            m_value.Left = lable3.Right + 5;
            m_value.Top = this.Location.Y;
            m_value.Width = 70;
            m_value.Height = lable3.Height;
            this.Controls.Add(m_value);

            LinkLabel ll = new LinkLabel();
            ll.Text = "删除";
            sizeF = graphics.MeasureString(ll.Text, font);
            ll.Width = (int)(Math.Ceiling(sizeF.Width)) + 1;
            ll.Height = (int)(Math.Ceiling(sizeF.Height));
            ll.Left = m_value.Right + 5;
            ll.Top = 3;
            ll.LinkClicked += new LinkLabelLinkClickedEventHandler(this.colLinkButton_Click);
            this.Controls.Add(ll);

            if (tb != null)
            {
                m_table.Text = tb.name;
                m_field.Text = tb.field;
                m_value.Text = tb.value;
            }
        }

        private void CreateControls(Font font, cause ca)
        {
            m_causeId.Left = this.Location.X;
            m_causeId.Top = 3;
            m_causeId.Width = 140;
            m_causeId.Height = 21;
            this.Controls.Add(m_causeId);

            LinkLabel ll = new LinkLabel();
            ll.Text = "删除";
            Graphics graphics = CreateGraphics();
            SizeF sizeF = graphics.MeasureString(ll.Text, font);
            ll.Width = (int)(Math.Ceiling(sizeF.Width)) + 1;
            ll.Height = (int)(Math.Ceiling(sizeF.Height));
            ll.Left = m_causeId.Right + 5;
            ll.Top = 3;
            ll.LinkClicked += new LinkLabelLinkClickedEventHandler(this.colLinkButton_Click);
            this.Controls.Add(ll);

            if (ca != null)
            {
                m_causeId.Text = ca.id;
            }
        }
        //public TextBox m_elementName = new TextBox();
        //public TextBox m_elementXpath = new TextBox();
        //public TextBox m_elementValue = new TextBox();
        private void CreateControls(Font font, element el)
        {
            Graphics graphics = CreateGraphics();
            Label lable1 = new Label();
            lable1.Left = this.Location.X;
            lable1.Text = "Xpath";
            SizeF sizeF = graphics.MeasureString(lable1.Text, font);
            lable1.Width = (int)(Math.Ceiling(sizeF.Width));
            lable1.Height = (int)(Math.Ceiling(sizeF.Height));
            lable1.Top = 3;
            this.Controls.Add(lable1);

            m_elementXpath.Left = lable1.Right+5;
            m_elementXpath.Top = this.Location.Y; ;
            m_elementXpath.Width = 140;
            m_elementXpath.Height = 21;
            this.Controls.Add(m_elementXpath);

            Label lable2 = new Label();
            lable2.Left = m_elementXpath.Right + 5;
            lable2.Text = "条件";
            sizeF = graphics.MeasureString(lable2.Text, font);
            lable2.Width = (int)(Math.Ceiling(sizeF.Width));
            lable2.Height = (int)(Math.Ceiling(sizeF.Height));
            lable2.Top = 3;
            this.Controls.Add(lable2);

            m_elementValue.Left = lable2.Right + 5;
            m_elementValue.Top = this.Location.Y; ;
            m_elementValue.Width = 140;
            m_elementValue.Height = 21;
            this.Controls.Add(m_elementValue);

            LinkLabel ll = new LinkLabel();
            ll.Text = "删除";
            sizeF = graphics.MeasureString(ll.Text, font);
            ll.Width = (int)(Math.Ceiling(sizeF.Width)) + 1;
            ll.Height = (int)(Math.Ceiling(sizeF.Height));
            ll.Left = m_elementValue.Right + 5;
            ll.Top = 3;
            ll.LinkClicked += new LinkLabelLinkClickedEventHandler(this.colLinkButton_Click);
            this.Controls.Add(ll);

            if (el != null)
            {
                m_elementXpath.Text = el.xpath;
                m_elementValue.Text = el.value;
            }
        }

        private void colLinkButton_Click(object sender, EventArgs e)
        {
            DeleteBtnClicked(this);
        }

        public table Get()
        {
            table table = new table();
            if (m_table.Text == "") return null;
            if (m_field.Text == "") return null;
            if (m_value.Text == "") return null;
            table.name = m_table.Text;
            table.field = m_field.Text;
            table.value = m_value.Text;
            return table;
        }

        public cause causeGet()
        {
            cause ca = new cause();
            if (m_causeId.Text == "") return null;
            ca.id = m_causeId.Text;
            return ca;
        }

        public element elementGet()
        {
            element el = new element();
            if (m_elementXpath.Text == "") return null;
            if (m_elementValue.Text == "") return null;

            el.xpath = m_elementXpath.Text;
            el.value = m_elementValue.Text;
            return el;
        }
    }
}
